<?php

namespace App\Models\Home;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use App\Models\Home\Users;

class Part extends Model
{
    //
    protected $table = 'part';
    protected $guarded = [];

    protected static function boot()
    {
        parent::boot();

        self::addGlobalScope("avaiable",function (Builder $query){
            $query->whereIn('status',[1]);
        });
    }

    public function user(){

        return $this->belongsTo('App\Models\Home\Users','uid','id');

    }

    public function assign_user(){

        return $this->belongsTo('App\Models\Home\Users','assign','id');

    }

    public function create_user(){

        return $this->belongsTo('App\Models\Home\Users','create_uid','id');

    }

    public function check_schedule(){

        return $this->hasOne('App\Models\Home\PartSchedule', 'part_id', 'id');
    }

    public function one_schedule(){

        return $this->hasOne('App\Models\Home\PartSchedule', 'part_id', 'id');
    }

    public function all_schedule(){

        return $this->hasMany('App\Models\Home\PartSchedule', 'part_id', 'id');
    }

    public function SetAssign($obj){
        $obj->load(['check_schedule'=>function($query){
            $query->orderBy('id','desc');
        }]);

        if ($obj->one_schedule && $obj->one_schedule->result == 4){
            return $obj;
        }

        $assign = $obj->assign;
        $assign_time = $obj->assign_time;
        $share = $obj->share;

        if($obj->assign){
            if($obj->assign_time <= time()){
                $obj->assign = '';
                $obj->assign_time = '';
                $obj->share = 1;
                //插一条跟进记录
            }else{
                $obj->share = 0;
            }
        }else{
            $obj->share = 1;
            $obj->assign = '';
            $obj->assign_time = '';
        }

        if($assign != $obj->assign||$assign_time != $obj->assign_time||$share != $obj->share){

            $obj->save();

        }

        return $obj;
    }


    public function getList($skip,$limit,$opt=array(),$in=array(),$user=true,$assign=true,$create=true,$schedule=1){

        if (!isset($limit)||!isset($skip)){
            return [];
        }
        if (!isset($skip)){
            $skip = 0;
        }

        $before = self::get();

        foreach ($before as $k => $v){

            $this->SetAssign($v);

        }

        $query = Part::orderBy('id', 'desc');

        if (!empty($opt)){
            foreach ($opt as $k => $v){
                if(is_array($v) && $v[1]){
                    $query = $query->where($k,$v[0],$v[1]);
                }elseif(!is_array($v) && $v !== false){
                    $query = $query->where($k,$v);
                }
            }
        }

        if (!empty($in) && count($in)==2){
            $query = $query->where(function ($query) use($in){
                $query->orWhere(function ($query) use($in){
                    $query->whereIn($in[0],$in[1]);
                });
                $query->orWhere('share', 1);
            });
        }

        $query = $query->skip($skip)->limit($limit);

        if ($user){
            $query = $query->with(['user']);
        }

        if ($assign){
            $query = $query->with(['assign_user']);
        }

        if ($create){
            $query = $query->with(['create_user']);
        }

        if ($schedule == 1){
            $query = $query->with(['one_schedule'=>function($query){
                $query->orderBy('id', 'desc')->first();
            }]);
        }elseif($schedule == 2){
            $query = $query->with(['all_schedule']);
        }

        $list = $query->get();

        return $list;
    }

    public function getCount($opt=array(),$in=array()){

        $query = Part::orderBy('id', 'desc');

        if (!empty($opt)){
            foreach ($opt as $k => $v){
                if(is_array($v) && $v[1]){
                    $query = $query->where($k,$v[0],$v[1]);
                }elseif(!is_array($v) && $v !== false){
                    $query = $query->where($k,$v);
                }
            }
        }

        if (!empty($in) && count($in)==2){
            $query = $query->whereIn($in[0],$in[1]);
        }

        return $query->count();
    }

    public function getPartUser($assign=array()){
        if (empty($assign)){
            return [];
        }

        $query = self::orderBy('id', 'desc');

        $res = $query->whereIn('assign',$assign)->where('assign_time','>',time())->get();

        $data = [];
        if ($res){
            foreach ($res as $item){
                if (intval($item->create_uid)){
                    $data[] = intval($item->create_uid);
                }
            }
        }

        return $data;

    }

    public function getPartUserInfo($assign=array()){
        if (empty($assign)){
            return [];
        }

        $create_uid_list = self::getPartUser($assign);

        $data = Users::where('status',1)->whereIn('id',$create_uid_list)->get();

        return $data;

    }

}
